﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SistemaAcademicoBO;
using System.Configuration;
using System.Data;
using SistemaAcademicoBO.Entities;

namespace SistemaAcademicoDAL
{
    public class ProfessorDAO : AbstratcDAO, IDAO<IEntity>
    {
        public ProfessorDAO()
        {
            connection = new Connection();
        }

        public IEntity GetRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveProfessor", connection.GetSQLConnection());

            procedureCall.Parameters.Add(new ProcedureParam("@ProfessorId", SqlDbType.Int, 0, id));

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(Professor));

            connection.CloseConnection();

            if (resultSet.Count != 1)
                return null;

            return resultSet[0];
        }

        public List<IEntity> GetAllRecords()
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveAllProfessors", connection.GetSQLConnection());

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(Professor));

            connection.CloseConnection();

            return resultSet;
        }

        public void SetRecord(IEntity entity)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();
            ProcedureCall procedureCall = new ProcedureCall("pr_storeProfessor", connection.GetSQLConnection());

            Professor professor = (Professor)entity;

            procedureCall.Parameters.Add(new ProcedureParam("@ProfessorId", SqlDbType.Int, 0, professor.ProfessorId));
            procedureCall.Parameters.Add(new ProcedureParam("@Nome", SqlDbType.VarChar, 250, professor.Nome));
            procedureCall.Parameters.Add(new ProcedureParam("@Rg", SqlDbType.VarChar, 20, professor.Rg));
            procedureCall.Parameters.Add(new ProcedureParam("@Cpf", SqlDbType.VarChar, 20, professor.Cpf));
            procedureCall.Parameters.Add(new ProcedureParam("@Inscricao", SqlDbType.VarChar, 100, professor.Inscricao));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }

        public void RemoveRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_removeProfessor", connection.GetSQLConnection());

            procedureCall.Parameters.Add(new ProcedureParam("@ProfessorId", SqlDbType.Int, 0, id));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }
    }
}
